package com.module.admin.app.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.app.dao.AppInfoDao;
import com.module.admin.app.pojo.AppInfo;
import com.module.admin.app.service.AppInfoService;
import com.module.admin.sys.pojo.SysUser;
import com.module.admin.sys.utils.SysUserUtil;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseFrame;

/**
 * app_info的Service
 * @author admin
 * @date 2020-12-27 20:12:45
 * @version V1.0.0
 */
@Component
public class AppInfoServiceImpl implements AppInfoService {

	@Autowired
	private AppInfoDao appInfoDao;
	
	/**
	 * 保存或修改
	 * @param appInfo
	 * @return
	 */
	@Override
	public ResponseFrame saveOrUpdate(AppInfo appInfo) {
		ResponseFrame frame = new ResponseFrame();
		if(FrameStringUtil.isEmpty(appInfo.getAiId())) {
			appInfo.setAiId(FrameNoUtil.uuid());
			appInfoDao.save(appInfo);
		} else {
			appInfoDao.update(appInfo);
		}
		frame.setSucc();
		return frame;
	}
	
	/**
	 * 根据aiId获取对象
	 * @param aiId
	 * @return
	 */
	@Override
	public AppInfo get(String aiId) {
		return handleDtl(appInfoDao.get(aiId));
	}

	/**
	 * 分页获取对象
	 * @param appInfo
	 * @return
	 */
	@Override
	public Page<AppInfo> pageQuery(AppInfo appInfo) {
		appInfo.setDefPageSize();
		int total = appInfoDao.findAppInfoCount(appInfo);
		List<AppInfo> data = null;
		if(total > 0) {
			data = appInfoDao.findAppInfo(appInfo);
			for (AppInfo item : data) {
				handleDtl(item);
			}
		}
		Page<AppInfo> page = new Page<AppInfo>(appInfo.getPage(), appInfo.getSize(), total, data);
		return page;
	}
	/**
	 * 处理字典等详细信息的方法
	 * @param appInfo
	 */
	private AppInfo handleDtl(AppInfo appInfo) {
		if (appInfo != null) {
		}
		return appInfo;
	}
	
	/**
	 * 根据aiId删除对象
	 * @param aiId
	 * @return
	 */
	@Override
	public ResponseFrame delete(String aiId) {
		ResponseFrame frame = new ResponseFrame();
		appInfoDao.delete(aiId);
		frame.setSucc();
		return frame;
	}

	@Override
	public Map<String, Object> getAppSum(SysUser user) {
		Integer userId = user.getUserId();
		if (SysUserUtil.isAuth(user, "appInfoQueryAll")) {
			// 查看所有权限
			userId = null;
		}
		return appInfoDao.getCount(userId);
	}
}